using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;

namespace UFODefense
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Game
    {

        Random m_randomizer;
        GraphicsDeviceManager m_graphics;
        SpriteBatch m_spriteBatch;
        Definitions m_definitions;
        Rectangle m_ground;
        Texture2D m_groundT;
        List <UFO> m_ufos;
        LaserGun m_gun;
        MissilePad m_missilepad;
        //in total game milliseconds
        double m_lastUFOCreation;
        Song m_song;


        public Game1()
        {
            m_graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            m_definitions = new Definitions(m_graphics.GraphicsDevice.Viewport);
            m_ground = new Rectangle(0, (int)Definitions.GROUND_Y, m_graphics.GraphicsDevice.Viewport.Width,
                m_graphics.GraphicsDevice.Viewport.Height - (int)Definitions.GROUND_Y);

            m_randomizer = new Random();
            m_ufos = new List<UFO>();
            m_lastUFOCreation = 0;
            this.IsMouseVisible = false;
            
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            m_spriteBatch = new SpriteBatch(GraphicsDevice);
            m_missilepad = new MissilePad(this, m_spriteBatch, Content, m_randomizer);
            //m_gun = new LaserGun(this, m_spriteBatch, Content, m_randomizer);

            m_song = Content.Load<Song>("Music/MainTheme");

            TimeSpan lol = m_song.Duration;

            m_groundT = Content.Load<Texture2D>("GFX/ground");

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (MediaPlayer.State == MediaState.Stopped)
            {
                MediaPlayer.Play(m_song);
            }

            if ((gameTime.TotalGameTime.TotalMilliseconds - m_lastUFOCreation) > Definitions.UFO_SPAWN_INTERVAL)
            {
                //Create a new ufo, update lastUfoCreation and add the UFO to the collision detection observer
                UFO newUFO = new UFO(this, m_spriteBatch, Content, m_ground, m_randomizer);
                m_ufos.Add(newUFO);
                m_lastUFOCreation = gameTime.TotalGameTime.TotalMilliseconds;
                UFOCollisionObserver.Instance.AttachUFO(newUFO);
            }
            //check for dead ufos and null the pointers
            for (int i = (m_ufos.Count -1); i >= 0; --i)
            {
                //If the UFO has been disabled
                if (!m_ufos[i].Enabled)
                {
                    m_ufos.RemoveAt(i);
                }
            }
            
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            
            m_spriteBatch.Begin();
            // TODO: Add your drawing code here
            //Draw the ground
            m_spriteBatch.Draw(m_groundT, m_ground, Color.White);

            base.Draw(gameTime);
            m_spriteBatch.End();
        }
    }
}
